package leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

//给定一个排序链表，删除所有重复的元素，使得每个元素只出现一次。
//示例 1:
//输入: 1->1->2
//输出: 1->2
//示例 2:
//输入: 1->1->2->3->3
//输出: 1->2->3
public class problems_083 {

    public static void main(String[] arge) {
        ListNode A = new ListNode(1);
        ListNode B = new ListNode(4);
        ListNode C = new ListNode(4);
        ListNode D = new ListNode(5);
        ListNode E = new ListNode(5);
        ListNode F = new ListNode(2);
        A.next = B;
        B.next = C;
        C.next = D;
        D.next = E;
        E.next = F;
        System.out.println(new Solution().deleteDuplicates(A));
    }
    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
        @Override
        public String toString() {
            StringBuffer str = new StringBuffer();
            ListNode node = this;
            while (null != node.next){
                if(str.length() != 0) str.append("->");
                str.append(node.val);
                node = node.next;
            }
            return "【" + this.val + "】" + str.toString();
        }
    }

    static class Solution {
        private List<Integer> list = new ArrayList<Integer>();
        public ListNode deleteDuplicates(ListNode head) {
            if(null == head) return head;
            ListNode node = head;
            list.add(node.val);
            while (null != node.next){
                ListNode nextNode = node.next;
                // 重复判断
                if (list.indexOf(nextNode.val) != -1){
                    // 重复时处理
                    node.next = nextNode.next;
                }else {
                    list.add(nextNode.val);
                    node = nextNode;
                }
            }
            return head;
        }
    }
}
